/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.books;

import com.nc.kyiv.yellow.sociallibrary.dao.BookDAO;
import com.nc.kyiv.yellow.sociallibrary.helper.MapBuilder;
import com.nc.kyiv.yellow.sociallibrary.similarUse.CommonClassesInterface;
import java.util.Map;

/**
 *
 * @author Alena
 */
public class SetBookRating implements CommonClassesInterface {

    private String userId;

    public SetBookRating(String userID) {
        this.userId = userID;
    }
    private String[] mapKeys = {"isbn", "rating"};
    private MapBuilder helper = new MapBuilder();
    private String resultString = null;

    @Override
    public String execute(String parameters) throws Exception {

        Map postParameters = helper.stringToMap(parameters, mapKeys, helper.getRequestSplitter());

        if (!userId.equals("guest")) {
            postParameters.put("userId", userId);
            System.out.println(postParameters.toString());
            try {
                if (postParameters != null) {
                    BookDAO book = new BookDAO();
                    resultString = book.setOrUpdateRating(postParameters);
                    System.out.println(resultString);
                }
            } catch (Exception ex) {
                if (ex.getMessage().equals("Something is wrong with Data Base")) {
                    return "failed";
                }
            }
            return "success";
        } else {
            return "failed";
        }

    }
}
